package service

import (
	"elflog/common/context"
	"elflog/common/dig"
	"elflog/component/nlog"
	"elflog/module/module-log/dao"
	"elflog/module/module-log/model"
	"sync"
)

type ModuleLogService struct {
	log *nlog.NLog
	dao *dao.LogDao

	saveLogChannel   chan *model.CreateLogModelForChannel
	waitSaveLog      sync.WaitGroup
	startSaveLogOnce sync.Once
	stopSaveLogOnce  sync.Once
}

func NewModuleLogService(log *nlog.NLog, dao *dao.LogDao) *ModuleLogService {
	var svc = &ModuleLogService{
		log:              log,
		dao:              dao,
		saveLogChannel:   make(chan *model.CreateLogModelForChannel, 5000),
		waitSaveLog:      sync.WaitGroup{},
		startSaveLogOnce: sync.Once{},
		stopSaveLogOnce:  sync.Once{},
	}
	svc.StartSaveWorker(context.NewContext())
	return svc
}

func init() {
	dig.Container.MustProvide(NewModuleLogService)
}
